﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Xml;
using System.Xml.Serialization;

namespace Carbon.Web.Forms
{
    public class FormsConfigurationSection : ConfigurationSection
    {
        private Configuration m_Instance;

        public Configuration Instance
        {
            get
            {
                return m_Instance;
            }
        }

        protected override void DeserializeSection(XmlReader reader)
        {
            XmlSerializer xs = new XmlSerializer(typeof(Configuration));

            m_Instance = (Configuration)xs.Deserialize(reader);
        }
    }


    [XmlRoot("carbon.forms")]
    public class Configuration
    {
        private static Configuration s_Instance;

        static Configuration()
        {
            s_Instance = ((FormsConfigurationSection)ConfigurationManager.GetSection("carbon.forms")).Instance;
            m_PersistanceServiceFactoryTypeInstanted = Type.GetType(s_Instance.PersistanceServiceFactoryType, true);
        }

        public static Configuration Instance
        {
            get
            {
                return s_Instance;
            }
        }

        private static Type m_PersistanceServiceFactoryTypeInstanted = null;
        [XmlIgnore]
        public Type PersistanceServiceFactoryTypeInstanted
        {
            get
            {
                if (m_PersistanceServiceFactoryTypeInstanted == null) throw new NullReferenceException("PersistanceServiceFactoryType");
                return m_PersistanceServiceFactoryTypeInstanted;
            }
            set
            {
                m_PersistanceServiceFactoryTypeInstanted = value;
            }
        }

        private String m_PersistanceServiceFactoryType = default(String);
        [XmlElement("PersistanceServiceFactoryType")]
        public String PersistanceServiceFactoryType
        {
            get
            {
                if (m_PersistanceServiceFactoryType == null) throw new NullReferenceException("PersistanceServiceFactoryType");
                return m_PersistanceServiceFactoryType;
            }
            set
            {
                m_PersistanceServiceFactoryType = value;
            }
        }

        private String m_PersistanceServiceConnectionStringName = default(String);
        [XmlElement("PersistanceServiceConnectionStringName")]
        public String PersistanceServiceConnectionStringName
        {
            get
            {
                if (string.IsNullOrEmpty(m_PersistanceServiceConnectionStringName)) throw new ArgumentOutOfRangeException("PersistanceServiceConnectionStringName");
                return m_PersistanceServiceConnectionStringName;
            }
            set
            {
                m_PersistanceServiceConnectionStringName = value;
            }
        }

        private String m_ControlsPath = default(String);
        [XmlElement("ControlsPath")]
        public String ControlsPath
        {
            get
            {
                if (string.IsNullOrEmpty(m_ControlsPath)) throw new ArgumentOutOfRangeException("ControlsPath");
                return m_ControlsPath;
            }
            set
            {
                m_ControlsPath = value;
            }
        }

        private String m_PagesPath = default(String);
        [XmlElement("PagesPath")]
        public String PagesPath
        {
            get
            {
                if (string.IsNullOrEmpty(m_PagesPath)) throw new ArgumentOutOfRangeException("PagesPath");
                return m_PagesPath;
            }
            set
            {
                m_PagesPath = value;
            }
        }

        private String m_FilesPath = default(String);
        [XmlElement("FilesPath")]
        public String FilesPath
        {
            get
            {
                if (string.IsNullOrEmpty(m_FilesPath)) throw new ArgumentOutOfRangeException("FilesPath");
                return m_FilesPath;
            }
            set
            {
                m_FilesPath = value;
            }
        }

        private String m_SkinCssUrl = default(String);
        [XmlElement("SkinCssUrl")]
        public String SkinCssUrl
        {
            get
            {
                return m_SkinCssUrl;
            }
            set
            {
                m_SkinCssUrl = value;
            }
        }

        [XmlIgnore]
        public String ConnectionStringValue
        {
            get
            {
                ConnectionStringSettings settings =  ConfigurationManager.ConnectionStrings[PersistanceServiceConnectionStringName];
                if (settings == null)
                    throw new InvalidOperationException(string.Format("Connection string named '{0}' not found", Configuration.Instance.PersistanceServiceConnectionStringName));
                
                if (string.IsNullOrEmpty(settings.ConnectionString)) throw new ArgumentOutOfRangeException("settings.ConnectionString");

                string value = settings.ConnectionString;
                return value;
            }
        }
    }
}
